package com.nyist.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.nyist.annotation.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 1.测试控制台
 *
 * @author yangxiaogen
 * @date 2023/3/2 17:14
 */
@Slf4j
@RestController
@RequestMapping("/Api")
public class BarrierGateController {
    @Value("${server.port}")
    private String port;

    // E7 三 2.1 包月车 下发
    @SysLog(value = "E7 三 2.1 包月车 下发")
    @PostMapping("/TokenService/Issue")
    public Map<String, Object> Issue(@RequestBody JSONObject params) {
        log.info("=====>>>Issue,接收入参为:{}", params);
        Map<String, Object> map = new HashMap<>();
        JSONObject state = new JSONObject(true);
        state.put("IsError", false);
        state.put("IsSucess", true);
        state.put("Code", 0);
        state.put("RecordAffected", 0);
        state.put("Describe", null);
        map.put("State", state);

        JSONObject model = new JSONObject(true);
        model.put("Date", null);
        model.put("Result", true);
        model.put("IsLegal", true);
        model.put("ErrCode", 0);
        model.put("ErrMessage", null);
        model.put("Gid", null);
        model.put("Rid", null);
        model.put("ID", 0);
        map.put("Model", model);

        return map;
    }

    // 4.3 获取 临时车信息
    @SysLog(value = "4.3 获取 临时车信息")
    @PostMapping("/Park/GetTempCarPaymentInfoByCarNo")
    public Map<String, Object> getTempCarPaymentInfoByCarNo(@RequestBody JSONObject params) {
        log.info("=====>>>GetTempCarPaymentInfoByCarNo,接收入参为:{}", params);
        Map<String, Object> map = new HashMap<>();
        map.put("IsSuccess", true);
        map.put("Redirect", null);
        map.put("MessageCode", 200);
        map.put("MessageContent", "请求成功");
        JSONObject result = new JSONObject(true);
        result.put("ParkingCode", params.getString("ParkingCode"));
        result.put("CarNo", params.getString("CarNo"));
        result.put("ParkingFee", new BigDecimal(RandomUtil.randomInt(5,100)));
        result.put("ActualAmount", new BigDecimal(RandomUtil.randomInt(5,100)));
        result.put("ParkingName", "厦门五缘湾乐都汇");
        result.put("CardType", "3");
        result.put("BeginTime", "2023-05-11 10:05:00");
        result.put("ChargeableTime", "2023-05-11 10:10:00");
        map.put("Result", result);
        return map;
    }

    // 4.10.1 临时车 缴费
    @SysLog(value = "4.10.1 临时车 缴费")
    @PostMapping("/CalculationCost/ApiThirdPartyTemporaryCardPay")
    public Map<String, Object> apiThirdPartyTemporaryCardPay(@RequestBody JSONObject params) {
        log.info("=====>>>ApiThirdPartyTemporaryCardPay,接收入参为:{}", params);
        Map<String, Object> map = new HashMap<>();
        map.put("IsSuccess", true);
        map.put("Redirect", null);
        map.put("MessageCode", 200);
        map.put("MessageContent", "请求成功");
        map.put("TradeNo", IdUtil.fastSimpleUUID());
        return map;
    }

    // 4.9.1 无牌车 入场
    @SysLog(value = "4.9.1 无牌车 入场")
    @PostMapping("/Park/UnlicensedCarsIn")
    public Map<String, Object> UnlicensedCarsIn(@RequestBody JSONObject params) {
        log.info("=====>>>UnlicensedCarsIn,接收入参为:{}", params);
        Map<String, Object> map = new HashMap<>();
        map.put("Redirect", null);
        map.put("IsSuccess", true);
        map.put("MessageCode", 200);
        map.put("MessageContent", "请求成功");
        map.put("Result", null);
        return map;
    }

    // 4.9.2 无牌车 出场
    @SysLog(value = "4.9.2 无牌车 出场")
    @PostMapping("/Park/UnlicensedCarsOut")
    public Map<String, Object> UnlicensedCarsOut(@RequestBody JSONObject params) {
        log.info("=====>>>UnlicensedCarsOut,接收入参为:{}", params);
        Map<String, Object> map = new HashMap<>();
        map.put("Redirect", null);
        map.put("IsSuccess", true);
        map.put("MessageCode", 200);
        map.put("MessageContent", "请求成功");
        JSONObject result = new JSONObject(true);
        result.put("ParkingCode", params.getString("ParkingCode"));
        result.put("CardNo", params.getString("CardNo"));
        result.put("ParkingFee", new BigDecimal(RandomUtil.randomInt(5,100)));
        result.put("ActualAmount", new BigDecimal(RandomUtil.randomInt(5,100)));
        result.put("ParkingName", "厦门五缘湾乐都汇");
        result.put("CardType", 3);
        result.put("BeginTime", "2023-05-11 10:05:00");
        result.put("ChargeableTime", "2023-05-11 10:10:00");
        map.put("Result", result);
        return map;
    }

    // 门禁 下发
    @SysLog(value = "门禁 下发")
    @PostMapping("/TokenService/DoorIssue")
    public Map<String, Object> doorIssue(@RequestBody JSONObject params) {
        // public Map<String,Object> doorIssue(HttpServletRequest request, HttpServletResponse response) {
        log.info("=====>>>DoorIssue,门禁 下发,接收入参为:{}", params);
        Map<String, Object> map = new HashMap<>();
        JSONObject state = new JSONObject(true);
        state.put("IsSucess", true);
        state.put("IsError", false);
        state.put("Code", 0);
        state.put("RecordAffected", 0);
        state.put("Describe", "响应信息");
        map.put("State", state);
        JSONObject model = new JSONObject(true);
        model.put("Result", true);
        model.put("IsLegal", true);
        model.put("Data", null);
        model.put("ErrCode", 0);
        model.put("ErrMessage", null);
        model.put("Gid", null);
        model.put("Rid", null);
        model.put("ID", 0);
        map.put("Model", model);

        map.put("Models", null);

        return map;
    }

    // 门禁 访客登记
    @SysLog(value = "门禁 访客登记")
    @PostMapping("/VisitorServicePro/AddVisitorAPP")
    public Map<String, Object> addVisitorAPP(@RequestBody JSONObject params, HttpServletRequest request, HttpServletResponse response) {
        // public Map<String,Object> doorIssue(HttpServletRequest request, HttpServletResponse response) {
        log.info("=====>>>AddVisitorAPP,门禁 访客登记,接收入参为:{}", params);
        Map<String, Object> map = new HashMap<>();
        JSONObject state = new JSONObject(true);
        state.put("IsSucess", true);
        state.put("IsError", false);
        state.put("Code", 0);
        state.put("RecordAffected", 0);
        state.put("Describe", "响应信息");
        map.put("State", state);
        JSONObject model = new JSONObject(true);
        model.put("Result", true);
        model.put("IsLegal", true);
        model.put("Data", null);
        model.put("ErrCode", 0);
        model.put("ErrMessage", null);
        model.put("Gid", null);
        model.put("Rid", null);
        model.put("ID", 0);
        map.put("Model", model);

        map.put("Models", null);
        return map;
    }

    // 门禁 注销
    @SysLog(value = "门禁 注销")
    @PostMapping("/TokenService/DoorUnregister")
    public Map<String, Object> doorUnregister(@RequestBody JSONObject params, HttpServletRequest request, HttpServletResponse response) {
        log.info("=====>>>DoorUnregister,门禁 注销,接收入参为:{}", params);

        Map<String, Object> map = new HashMap<>();
        map.put("IsSucess", true);
        map.put("IsError", false);
        map.put("Code", 0);
        map.put("RecordAffected", 1);
        map.put("Describe", "sample string 2");

        return map;
    }

    private Map<String,Object> getParamsByHttpServletRequest(HttpServletRequest request) {
        Map<String,Object> params = new HashMap<>();
        String method = request.getMethod();
        if (StringUtils.equals(String.valueOf(HttpMethod.GET), method)) {
            log.info("=====>>>GET 请求<<<=====");
            Map<String, String[]> parameterMap = request.getParameterMap();
            parameterMap.forEach((key, value) -> log.info("key: {} , value: {}", key, value));
        } else if (StringUtils.equals(String.valueOf(HttpMethod.POST), method)) {
            log.info("=====>>>POST 请求<<<=====");
            Enumeration<String> requestParameterNames = request.getParameterNames();
            if (null != requestParameterNames) {
                while (requestParameterNames.hasMoreElements()) {
                    String key = requestParameterNames.nextElement();
                    String value = request.getParameter(key);
                    params.put(key, value);
                }
            }
        } else {
            log.warn("=====>>>other 请求,类型为 : {}<<<=====", method);
        }

        return params;
    }
}
